<!DOCTYPE html>
<html>
<head>
<title>角色管理</title>
<#include "/header.html">
</head>
<body>
<div id="rrapp" v-cloak>
	<div v-show="showList">
		<div class="grid-btn">
			<div class="form-group col-sm-2">
				<input type="text" class="form-control" v-model="q.roleName" @keyup.enter="query" placeholder="角色名称">
			</div>
			<a class="btn btn-default" @click="query">查询</a>

			<#if shiro.hasPermission("sys:role:save")>
			<a class="btn btn-primary" @click="add"><i class="fa fa-plus"></i>&nbsp;新增</a>
			</#if>
			<#if shiro.hasPermission("sys:role:update")>
			<a class="btn btn-primary" @click="update"><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>
			</#if>
			<#if shiro.hasPermission("sys:role:delete")>
			<a class="btn btn-primary" @click="del"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
			</#if>
		</div>
	    <table id="jqGrid"></table>
	    <div id="jqGridPager"></div>
    </div>

	<div v-show="!showList" class="panel panel-default">
		<div class="panel-heading">{{title}}</div>
		<form class="form-horizontal">
			<div class="form-group">
				<div class="col-sm-2 control-label">角色名称</div>
				<div class="col-sm-10">
					<input type="text" class="form-control" v-model="role.roleName" placeholder="角色名称"/>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-2 control-label">所属部门</div>
				<div class="col-sm-10">
					<input type="text" class="form-control" style="cursor:pointer;" v-model="role.deptName" @click="deptTree" readonly="readonly" placeholder="所属部门"/>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-2 control-label">备注</div>
				<div class="col-sm-10">
					<input type="text" class="form-control" v-model="role.remark" placeholder="备注"/>
				</div>
			</div>
			<div class="form-inline clearfix" style="margin-top:30px;margin-left:26px;">
				<div class="form-group col-md-6">
					<strong class="col-sm-5 control-label">功能权限</strong>
					<div class="col-sm-10">
						<ul id="menuTree" class="ztree"></ul>
					</div>
				</div>
				<div class="form-group col-md-6">
					<strong class="col-sm-5 control-label">数据权限</strong>
					<div class="col-sm-10">
						<ul id="dataTree" class="ztree"></ul>
					</div>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-2 control-label"></div>
				<input type="button" class="btn btn-primary" @click="saveOrUpdate" value="确定"/>
				&nbsp;&nbsp;<input type="button" class="btn btn-warning" @click="reload" value="返回"/>
			</div>
		</form>
	</div>
</div>

<!-- 选择部门 -->
<div id="deptLayer" style="display: none;padding:10px;">
		<div class="wrapper">
	<div class="treeShowHideButton" onclick="search();">
		<label id="btnShow" title="显示搜索" style="display:none;">︾</label>
		<label id="btnHide" title="隐藏搜索">︽</label>
	</div>
	<div class="treeSearchInput" id="search">
		<label for="keyword">关键字：</label><input type="text" class="empty" id="keyword" maxlength="50">
		<button class="btn" id="btn" onclick="searchNode()"> 搜索 </button>
	</div>
	<div class="treeExpandCollapse">
		<a href="javascript:" id="btnExpand">展开</a> /
		<a href="javascript:" id="btnCollapse">折叠</a>
	</div>
	<div id="deptTree" class="ztree"></div>
	</div>
</div>
<script src="${request.contextPath}/statics/js/modules/sys/role.js?_${.now?long}"></script>
<script>
var lastValue = "", nodeList = [], key = $("#keyword");
key.bind("focus", focusKey).bind("blur", blurKey).bind("change cut input propertychange", searchNode);
key.bind("keydown", function (e){if(e.which == 13){searchNode();}});

function focusKey(e) {
	if (key.hasClass("empty")) {
		key.removeClass("empty");
	}
}

function blurKey(e) {
	if (key.get(0).value === "") {
		key.addClass("empty");
	}
	searchNode(e);
}

function searchNode() {
	var value = $.trim(key.get(0).value);
	var keyType = "name";
	if (lastValue === value) {
		return;
	}
	lastValue = value;
	var nodes = dept_ztree.getNodes();
	if (value == "") {
		showAllNode(nodes);
		return;
	}
	hideAllNode(nodes);
	nodeList = dept_ztree.getNodesByParamFuzzy(keyType, value);
	updateNodes(nodeList);
}

function hideAllNode(nodes){
	//var ztree = $.fn.zTree.getZTreeObj("deptTree");
	nodes = dept_ztree.transformToArray(nodes);
	for(var i=nodes.length-1; i>=0; i--) {
		dept_ztree.hideNode(nodes[i]);
	}
}

function showAllNode(nodes){
	nodes = dept_ztree.transformToArray(nodes);
	for(var i=nodes.length-1; i>=0; i--) {
		if(nodes[i].getParentNode()!=null){
			dept_ztree.expandNode(nodes[i],false,false,false,false);
		}else{
			dept_ztree.expandNode(nodes[i],true,true,false,false);
		}
		dept_ztree.showNode(nodes[i]);
		showAllNode(nodes[i].children);
	}
}

function updateNodes(nodeList) {
	dept_ztree.showNodes(nodeList);
	for(var i=0, l=nodeList.length; i<l; i++) {
		var treeNode = nodeList[i];
		showChildren(treeNode);
		showParent(treeNode)
	}
}

function showChildren(treeNode){
	if (treeNode.isParent){
		for(var idx in treeNode.children){
			var node = treeNode.children[idx];
			dept_ztree.showNode(node);
			showChildren(node);
		}
	}
}
function showParent(treeNode){
	var parentNode;
	while((parentNode = treeNode.getParentNode()) != null){
		dept_ztree.showNode(parentNode);
		dept_ztree.expandNode(parentNode, true, false, false);
		treeNode = parentNode;
	}
}

function search($this) {
	$('#search').slideToggle(200);
	$('#btnShow').toggle();
	$('#btnHide').toggle();
	$('#keyword').focus();
}

$(function () {
    
    $('#btnExpand').click(function() {
    	dept_ztree.expandAll(true);
	});
	$('#btnCollapse').click(function() {
		dept_ztree.expandAll(false);
	});
});
</script>
</body>
</html>